package com.service.business.notify.controller;

import com.service.business.framework.log.RequestLog;
import com.service.business.notify.controller.req.CountNotReadNumReq;
import com.service.business.notify.controller.req.ReadTargetReq;
import com.service.business.notify.service.NotifyMessageService;
import com.service.business.support.SessionUtil;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "消息通知")
@RestController
@RequestMapping("notify")
public class NotifyController extends BaseController {

    @Autowired
    private NotifyMessageService notifyMessageService;

    @Autowired
    private SessionUtil sessionUtil;

    @ApiOperation("统计用户某类型未读消息数")
    @RequestLog("统计用户某类型未读消息数")
    @PostMapping("totalNotByType")
    public BaseResponse totalByType(@ApiParam @RequestBody CountNotReadNumReq req){
        Integer memberId = sessionUtil.getUser().getUserId();

        return successQueryOneResponse(notifyMessageService.countNotReadByMemberIdAndType(memberId, req.getMessageType()));
    }

    @ApiOperation("用户操作某类型消息全部已读")
    @RequestLog("用户操作某类型消息全部已读")
    @PostMapping("readAllByType")
    public BaseResponse readAllByType(@ApiParam @RequestBody CountNotReadNumReq req){
        Integer memberId = sessionUtil.getUser().getUserId();
        return successQueryOneResponse(notifyMessageService.readAllByMemberIdAndType(memberId, req.getMessageType()));
    }

    @ApiOperation("用户某消息已读")
    @RequestLog("用户某消息已读")
    @PostMapping("target")
    public BaseResponse target(@ApiParam @RequestBody ReadTargetReq req){
        Integer memberId = sessionUtil.getUser().getUserId();
        return successQueryOneResponse(notifyMessageService.readByMemberAndTypeAndTarget(memberId, req.getMessageType(), req.getTargetId()));
    }

}
